// pages/booking/calendar.js
var common = require('../../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      status:[],
      special:[],
      priceSet:null,
      today:'',
      rid:0
  },
  dateData: function () {
    let dataAll = []//总日历数据
    let dataAll2 = []//总日历数据
    let dataMonth = []//月日历数据
    let date = new Date(this.data.today)//当前日期
    let year = date.getFullYear()//当前年
    let week = date.getDay();//当天星期几
    let weeks = []
    let month = date.getMonth() + 1//当前月份
    let day = date.getDate()//当天
    let daysCount = 30//一共显示多少天
    let dayscNow = 0//计数器
    let monthList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]//月份列表
    let nowMonthList = []//本年剩余月份
    for (let i = month; i < 13; i++) {
      nowMonthList.push(i)
    }
    let yearList = [year]//年份最大可能
    for (let i = 0; i < daysCount / 365 + 2; i++) {
      yearList.push(year + i + 1)
    }
    let leapYear = function (Year) {//判断是否闰年 
      if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
      } else { return (false); }
    }
    for (let i = 0; i < yearList.length; i++) {//遍历年
      let mList
      if (yearList[i] == year) {//判断当前年份
        mList = nowMonthList
      } else {
        mList = monthList
      }
      //console.log(mList)
      monthloop:
      for (let j = 0; j < mList.length; j++) {//循环月份
        //console.log('month:',mList[j]);
        let dataMonth = []
        let t_days = [31, 28 + leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        let t_days_thisYear = []
        if (yearList[i] == year) {
          for (let m = 0; m < nowMonthList.length; m++) {
            t_days_thisYear.push(t_days[mList[m] - 1])
          }
          t_days = t_days_thisYear
        } else {
          t_days = [31, 28 + leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        }
        for (let k = 0; k < t_days[j]; k++) {//循环每天
          let nowData
          if (dayscNow < daysCount) {//如果计数器没满
            let days = k + 1
            if (days < 10) {
              days = "0" + days
            }
            let theMonth = mList[j];
            if(theMonth<10){
              theMonth = "0" + theMonth;
            }
            let date = yearList[i] + "-" + theMonth + "-" + days;

            if (yearList[i] == year && mList[j] == month) {//判断当年当月
              if (k + 1 >= day) {//当年当月之后（含）的日子才显示，过滤掉之前的日子
                week=week%7;
                nowData = {
                  year: yearList[i],
                  month: mList[j],
                  day: k + 1,
                  date: date,
                  selected: 0,           
                }
                if (this.data.status && typeof this.data.status[date] !='undefined'){                  
                  nowData.roomStatus = this.data.status[date];
                }else{
                  if (this.data.special && typeof this.data.special[date] != 'undefined') {
                    nowData.price = this.data.special[date];
                  }else{
                    let isworkday=true;
                    if (week == 5 || week == 6) {
                      isworkday = false
                    }

                    if (this.data.priceSet.devide != '0000-00-00' && common.CompareDate(date, this.data.priceSet.devide) >= 0){
                      if (isworkday){
                        nowData.price = this.data.priceSet.workDayPrice2;
                      }else{
                        nowData.price = this.data.priceSet.weekEndPrice2;
                      }
                    }else{                    
                        if (isworkday) {
                          nowData.price = this.data.priceSet.workDayPrice;
                        } else {
                          nowData.price = this.data.priceSet.weekEndPrice;
                        }
                    }
                  }
                }
    
                week++;
                dayscNow++;
                dataMonth.push(nowData);
                if (k + 1 == day) {
                  let date = new Date(yearList[i] + "-" + mList[j] + "-" + (k + 1))
                  let weekss = date.getDay()//获取每个月第一天是周几
                  weeks.push(weekss)
                }
              }
            } else {//其他情况
              week = week % 7;
              nowData = {//组装自己需要的数据
                year: yearList[i],
                month: mList[j],
                day: k + 1,
                date: date,
                selected: 0,
               }
              if (this.data.status && typeof this.data.status[date] != 'undefined') {
                nowData.roomStatus = this.data.status[date];
              } else {
                if (this.data.special && typeof this.data.special[date] != 'undefined') {
                  nowData.price = this.data.special[date];
                } else {
                  let isworkday = true;
                  if (week == 5 || week == 6) {
                    isworkday = false
                  }

                  if (this.data.priceSet.devide != '0000-00-00' && common.CompareDate(date, this.data.priceSet.devide) >= 0) {
                    if (isworkday) {
                      nowData.price = this.data.priceSet.workDayPrice2;
                    } else {
                      nowData.price = this.data.priceSet.weekEndPrice2;
                    }
                  } else {
                    if (isworkday) {
                      nowData.price = this.data.priceSet.workDayPrice;
                    } else {
                      nowData.price = this.data.priceSet.weekEndPrice;
                    }
                  }
                }
              }
              


              week++
              dayscNow++
              dataMonth.push(nowData)
              if (k == 0) {
                let date = new Date(yearList[i] + "-" + mList[j] + "-" + k + 1)
                let weekss = date.getDay()//获取每个月第一天是周几
                weeks.push(weekss)
              }
            }
          } else {
            console.log('----------------', dataMonth)
            dataAll.push(dataMonth)
            break monthloop;
          }
        }
        //console.log('----------------',dataMonth)
        dataAll.push(dataMonth)
      }
    }
    console.log(dataAll);
    for (let i = 0; i < dataAll.length; i++) {
      if (dataAll[i].length != 0) {
        dataAll2.push(dataAll[i]);
      }
    }
    this.setData({
      date: dataAll2,
      weeks: weeks,
      pagetype:'day'
    })
  },  
  getPriceStatus(){
    var that=this;
    if(that.data.rid==0){
      return false;
    }
    common.request({
      url: 'c=Game&a=getRoomPriceStatus',
      data: {
        rid: that.data.rid
      },
      method: 'POST',
      header: { 'content-type': 'application/json' },
      success: function (ret) {
          if(ret.status!=0){
            wx.showToast({
              title: ret.msg,
            })
            return false;
          }
          that.data.status = ret.roomStatus;
          that.data.special = ret.special;
          that.data.priceSet=ret.set;
          that.data.today = ret.today;
          //console.log(that.data.status)
          that.dateData();
      },
      fail: function (error) {
        console.info(error);
      }
    })
  },
  selectday: function (e) {
    let index = e.currentTarget.dataset.index;
    let index2 = e.currentTarget.dataset.indexs;
    let day = this.data.date[index][index2];
    if (typeof day.roomStatus != 'undefined' && day.roomStatus != '0') {
      wx.showToast({
        image:'../../image/warn.png',
        title: '已被预订',
      })
      return false;
    }


    this.data.date[index][index2]['selected'] = 1 - day['selected'];
    this.setData({
      date: this.data.date
    })
  },
  submitDay:function(){
    let that=this;
    let selectDate='';
    let price='';
    for(let i=0;i<that.data.date.length;i++){
      for (let j = 0; j < that.data.date[i].length;j++){
        if (that.data.date[i][j]['selected']==1){
          selectDate += ',' + that.data.date[i][j]['date'];
          price += ',' + that.data.date[i][j]['price'];
        }
      }
    }
    if (selectDate==''){
      wx.showToast({
        icon:'none',
        title: '请选定日期',
      })
      return false;
    }

    wx.navigateTo({
      url: 'order?rid=' + that.data.rid + '&date=' + selectDate+'&price='+price
    })

   

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.rid=options.rid;
    this.getPriceStatus();
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})